import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:hx_demo_example/page/customer_service_page.dart';

import 'data/service_data.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home()
    );
  }
}


class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HomeState();
  }
}

class _HomeState extends State<Home> {

  @override
  void initState() {
    super.initState();

    initHxState();

  }

  void initHxState() async {


    try {
      String? str =  await ChatService.init();
      print("-------$str");
    } on PlatformException {
      print("error");
    }

    if (!mounted) return;
  }



  void registerHxState() async {

    try {
      String? str =  await ChatService.register();
      print("-------$str");
    } on PlatformException {
      print("error");
    }
    if (!mounted) return;
  }



  void loginHxState(BuildContext context) async {

    try {
      ChatService.login().then((value){
        print("-------$value");
        Navigator.push(context, MaterialPageRoute(builder: (BuildContext ctx) => const ChatPage()));
      });
    } on PlatformException {
      print("error");
    }
    if (!mounted) return;
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Column(
        children: [

          MaterialButton(onPressed: (){
            registerHxState();
          },child: const Text("注册"),),

          MaterialButton(onPressed: (){
            loginHxState(context);
          },child: const Text("登录"),),

        ],
      ),
    );
  }


}